package com.wwwarehouse.taskcenter.custom_view;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.KeyBoardItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by clannad.shao on 2017/11/15.
 * Description:
 */

public class CarNumDialog extends Dialog {

    private Params params;

    public CarNumDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void setParams(CarNumDialog.Params params) {
        this.params = params;
    }

    public void setSelection(String itemValue) {
        if (params.dataList.size() > 0) {
            int idx = params.dataList.indexOf(itemValue);
            if (idx >= 0) {
                params.initSelection = idx;
//                    params.loopData.setSelectOptions(params.initSelection);
            }
        }
    }

    public interface OnSelectedListener {
        void onSelected(String itemValue, int pos);

        void onCancel();
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
            /* 触摸外部弹窗 */
        if (isOutOfBounds(getContext(), event)) {
            if (params.callback != null) {
                params.callback.onCancel();
            }
        }
        return super.onTouchEvent(event);
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }


    private static final class Params {
        private boolean shadow = true;
        private boolean canCancel = true;
        private int initSelection;
        private OnSelectedListener callback;
        private final List<KeyBoardItemBean> dataList = new ArrayList<>();
        private View contentView;
    }

    public static class Builder {
        private final Context context;
        private final CarNumDialog.Params params;
        private String chooseName;
        private int pos;

        public Builder(Context context) {
            this.context = context;
            params = new CarNumDialog.Params();
        }

        private final String getCurrDateValue() {
//                return String.valueOf(params.dataList.get(params.loopData.getCurrentItems())).trim();
            return null;
        }

        public Builder setData(List<KeyBoardItemBean> dataList) {
            params.dataList.clear();
            params.dataList.addAll(dataList);
            return this;
        }


        public Builder setSelection(int selection) {
            params.initSelection = selection;
            return this;
        }

        public Builder setSelection(String selection) {
            if (params.dataList.size() > 0) {
                int idx = params.dataList.indexOf(selection);
                if (idx >= 0) {
                    params.initSelection = idx;
                }
            }
            return this;
        }


        public Builder setOnDataSelectedListener(OnSelectedListener onDataSelectedListener) {
            params.callback = onDataSelectedListener;
            return this;
        }

        public Builder setContentView(View view) {
            params.contentView = view;
            return this;
        }

        public CarNumDialog create() {
            final CarNumDialog dialog = new CarNumDialog(context, params.shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
            GridView gridView = (GridView) params.contentView.findViewById(R.id.idCarNumGv);
            final CarNumAdapter carNumAdapter = new CarNumAdapter(context, params.dataList, params.initSelection);
            gridView.setAdapter(carNumAdapter);
            TextView idCarNumConfirmBt = (TextView) params.contentView.findViewById(R.id.idCarNumConfirmBt);
            idCarNumConfirmBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    params.callback.onSelected(chooseName, pos);
                }
            });
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    pos = position;
                    if (params.dataList == null || params.dataList.size() - 1 < position || params.dataList.get(position) == null) {
                        chooseName = "浙";
                        return;
                    }
                    chooseName = params.dataList.get(position).getName();
                    carNumAdapter.setInitSelection(position);
                    carNumAdapter.notifyDataSetChanged();
                }
            });

            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setGravity(Gravity.BOTTOM);
            win.setWindowAnimations(R.style.Animation_Bottom_Rising);

            dialog.setContentView(params.contentView);
            dialog.setCanceledOnTouchOutside(params.canCancel);
            dialog.setCancelable(params.canCancel);

//                params.loopData = loopData;
            dialog.setParams(params);

            return dialog;
        }
    }

}
